package com.tuanzi.loan.business.repository.xhh.settlementsystem.impl;

import com.tuanzi.loan.business.repository.xhh.settlementsystem.custom.XhhSettlementIncomeRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.xhh.settlementsystem.XhhSettlementIncomeRequest;
import com.tuanzi.loan.vo.response.xhh.settlementsystem.XhhSettlementIncomeResponse;
import org.jooq.Record;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.tables.XhhLoanSettlementIncome.XHH_LOAN_SETTLEMENT_INCOME;

/**
 * @author caiwanlin
 * @create 2018-08-16 16:21
 */
public class XhhSettlementIncomeRepositoryImpl extends JOOQBaseRepositoryPlus implements XhhSettlementIncomeRepositoryCustom {
	@Override
	protected TableImpl getTable() {
		return XHH_LOAN_SETTLEMENT_INCOME;
	}

	@Override
	public Page<XhhSettlementIncomeResponse> list(XhhSettlementIncomeRequest searchRequest, Pageable pageable) {
		Long productId = searchRequest.getProductId();
		Long companyId = searchRequest.getCompanyId();
		Integer spreadMode = searchRequest.getSpreadMode();
		Integer spreadChannel = searchRequest.getSpreadChannel();
		Integer copMode = searchRequest.getCopMode();
		Integer settlementMode = searchRequest.getSettlementMode();
		Integer billStatus = searchRequest.getBillStatus();
		String creator = searchRequest.getCreator();
		Date startTime = searchRequest.getStartTime();
		Date endTime = searchRequest.getEndTime();

		fields(XHH_LOAN_SETTLEMENT_INCOME.fields());
		eq(XHH_LOAN_SETTLEMENT_INCOME.PRODUCT_ID, productId);
		eq(XHH_LOAN_SETTLEMENT_INCOME.COMPANY_ID, companyId);
		eq(XHH_LOAN_SETTLEMENT_INCOME.SPREAD_MODE, spreadMode);
		eq(XHH_LOAN_SETTLEMENT_INCOME.CREATOR, creator);
		eq(XHH_LOAN_SETTLEMENT_INCOME.SPREAD_CHANNEL, spreadChannel);
		eq(XHH_LOAN_SETTLEMENT_INCOME.COP_MODE, copMode);
		eq(XHH_LOAN_SETTLEMENT_INCOME.SETTLEMENT_MODE, settlementMode);
		eq(XHH_LOAN_SETTLEMENT_INCOME.BILL_STATUS, billStatus);
		between(XHH_LOAN_SETTLEMENT_INCOME.START_TIME, startTime, endTime);
		orderBy(XHH_LOAN_SETTLEMENT_INCOME.CREATE_TIME, Sort.Direction.DESC);
		Page<XhhSettlementIncomeResponse> search = search(pageable, record -> {
			XhhSettlementIncomeResponse response = new XhhSettlementIncomeResponse();
			response.setId(record.get(XHH_LOAN_SETTLEMENT_INCOME.ID));
			response.setProductId(record.get(XHH_LOAN_SETTLEMENT_INCOME.PRODUCT_ID));
			response.setProductName(record.get(XHH_LOAN_SETTLEMENT_INCOME.PRODUCT_NAME));
			response.setCompanyId(record.get(XHH_LOAN_SETTLEMENT_INCOME.COMPANY_ID));
			response.setCompanyName(record.get(XHH_LOAN_SETTLEMENT_INCOME.COMPANY_NAME));
			response.setIncomeType(record.get(XHH_LOAN_SETTLEMENT_INCOME.INCOME_TYPE));
			response.setStartTime(record.get(XHH_LOAN_SETTLEMENT_INCOME.START_TIME));
			response.setEndTime(record.get(XHH_LOAN_SETTLEMENT_INCOME.END_TIME));
			response.setSpreadMode(record.get(XHH_LOAN_SETTLEMENT_INCOME.SPREAD_MODE));
			response.setSpreadChannel(record.get(XHH_LOAN_SETTLEMENT_INCOME.SPREAD_CHANNEL));
			response.setCopMode(record.get(XHH_LOAN_SETTLEMENT_INCOME.COP_MODE));
			response.setPriceCpa(record.get(XHH_LOAN_SETTLEMENT_INCOME.PRICE_CPA));
			response.setPriceCps(record.get(XHH_LOAN_SETTLEMENT_INCOME.PRICE_CPS));
			response.setRegisterCount(record.get(XHH_LOAN_SETTLEMENT_INCOME.REGISTER_COUNT));
			response.setClickCount(record.get(XHH_LOAN_SETTLEMENT_INCOME.CLICK_COUNT));
			response.setLendAmount(record.get(XHH_LOAN_SETTLEMENT_INCOME.LEND_AMOUNT));
			response.setIncomeAmount(record.get(XHH_LOAN_SETTLEMENT_INCOME.INCOME_AMOUNT));
			response.setParticipateUv(record.get(XHH_LOAN_SETTLEMENT_INCOME.PARTICIPATE_UV));
			response.setUvValue(record.get(XHH_LOAN_SETTLEMENT_INCOME.UV_VALUE));
			response.setSettlementMode(record.get(XHH_LOAN_SETTLEMENT_INCOME.SETTLEMENT_MODE));
			response.setPayMode(record.get(XHH_LOAN_SETTLEMENT_INCOME.PAY_MODE));
			response.setBillId(record.get(XHH_LOAN_SETTLEMENT_INCOME.BILL_ID));
			response.setBillStatus(record.get(XHH_LOAN_SETTLEMENT_INCOME.BILL_STATUS));
			response.setRemark(record.get(XHH_LOAN_SETTLEMENT_INCOME.REMARK));
			response.setCreator(record.get(XHH_LOAN_SETTLEMENT_INCOME.CREATOR));
			response.setCreateTime(record.get(XHH_LOAN_SETTLEMENT_INCOME.CREATE_TIME));
			response.setUpdator(record.get(XHH_LOAN_SETTLEMENT_INCOME.UPDATOR));
			response.setUpdateTime(record.get(XHH_LOAN_SETTLEMENT_INCOME.UPDATE_TIME));
			response.setVersion(record.get(XHH_LOAN_SETTLEMENT_INCOME.VERSION));
			return response;
		});

		return search;
	}

	@Override
	public void updateCompanyNameByCompanyId(Long companyId, String companyName) {
		dslContext.update(XHH_LOAN_SETTLEMENT_INCOME).set(XHH_LOAN_SETTLEMENT_INCOME.COMPANY_NAME, companyName).where(XHH_LOAN_SETTLEMENT_INCOME.COMPANY_ID.eq(companyId)).execute();
	}

	@Override
	public void updateProductNameByProductId(Long productId, String productName) {
		dslContext.update(XHH_LOAN_SETTLEMENT_INCOME).set(XHH_LOAN_SETTLEMENT_INCOME.PRODUCT_NAME, productName).where(XHH_LOAN_SETTLEMENT_INCOME.PRODUCT_ID.eq(productId)).execute();
	}

	@Override
	public void updateCompanyIdAndCompanyNameByCompanyId(Long companyId, String companyName, Long newCompanyId) {
		dslContext.update(XHH_LOAN_SETTLEMENT_INCOME).set(XHH_LOAN_SETTLEMENT_INCOME.COMPANY_NAME, companyName).set(XHH_LOAN_SETTLEMENT_INCOME.COMPANY_ID, newCompanyId)
				.where(XHH_LOAN_SETTLEMENT_INCOME.COMPANY_ID.eq(companyId)).execute();
	}

	@Override
	public Double sumRangeTotalAmount(XhhSettlementIncomeRequest request) {
		Record record = dslContext.select(DSL.sum(XHH_LOAN_SETTLEMENT_INCOME.INCOME_AMOUNT)).from(XHH_LOAN_SETTLEMENT_INCOME)
				.where(XHH_LOAN_SETTLEMENT_INCOME.START_TIME.between(new java.sql.Date(request.getStartTime().getTime()), new java.sql.Date(request.getEndTime().getTime())))
				.fetchOne();
		return (record == null ||record.get(0) == null) ? 0.0 : record.into(Double.class);
	}

	@Override
	public Double sumRangeTotalUvValue(XhhSettlementIncomeRequest request) {
		Record record = dslContext.select(DSL.sum(XHH_LOAN_SETTLEMENT_INCOME.UV_VALUE)).from(XHH_LOAN_SETTLEMENT_INCOME)
				.where(XHH_LOAN_SETTLEMENT_INCOME.START_TIME.between(new java.sql.Date(request.getStartTime().getTime()), new java.sql.Date(request.getEndTime().getTime())))
				.fetchOne();
		return (record == null ||record.get(0) == null) ? 0.0 : record.into(Double.class);
	}
}
